package com.qdc.datacenters.controller;

import cn.hutool.core.lang.tree.Tree;
import com.qdc.datacenters.annotation.Log;
import com.qdc.datacenters.domain.ResponseResult;
import com.qdc.datacenters.domain.entity.SysDept;
import com.qdc.datacenters.domain.query.dept.SysDeptAddQuery;
import com.qdc.datacenters.domain.query.dept.SysDeptQuery;
import com.qdc.datacenters.enums.BusinessTypeEnum;
import com.qdc.datacenters.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author danmo
 * @date 2024-02-26 18:23
 **/
@Tag(name = "部门管理")
@RestController
@RequestMapping("/system/dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService iSysDeptService;

    @Log(title = "新增部门",businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@authz.hasPerm('system:dept:add')")
    @Operation(summary = "新增部门", method = "POST")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated SysDeptAddQuery query) {
        iSysDeptService.add(query);
        return ResponseResult.success();
    }


    @Log(title = "修改部门",businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@authz.hasPerm('system:dept:edit')")
    @Operation(summary = "修改部门", method = "POST")
    @PostMapping("/edit/{deptId}")
    public ResponseResult edit(@PathVariable("deptId") Long deptId, @RequestBody @Validated SysDeptAddQuery query) {
        query.setDeptId(deptId);
        iSysDeptService.edit(query);
        return ResponseResult.success();
    }

    @Log(title = "删除部门",businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@authz.hasPerm('system:dept:delete')")
    @Operation(summary = "删除部门", method = "POST")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody SysDeptQuery query) {
        iSysDeptService.delete(query);
        return ResponseResult.success();
    }

    @Log(title = "部门详情",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('system:dept:get')")
    @Operation(summary = "部门详情", method = "POST")
    @PostMapping("/get/{deptId}")
    public ResponseResult<SysDept> get(@PathVariable("deptId") Long deptId, @RequestBody SysDeptQuery query) {
        return ResponseResult.success(iSysDeptService.getById(deptId));
    }

    @Log(title = "部门列表",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('system:dept:list')")
    @Operation(summary = "部门列表", method = "POST")
    @PostMapping("/list")
    public ResponseResult<SysDept> list(@RequestBody SysDeptQuery query) {
        List<SysDept> sysDeptList = iSysDeptService.list(query);
        List<Tree<Long>> treeList = iSysDeptService.buildDeptTree(sysDeptList);
        return ResponseResult.success();
    }
}
